#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>

int main()
{
    pid_t pid = fork();
    if(pid == 0)
    {
        printf("Child started, will sleep 2s\n");
        sleep(2);
        printf("Child exiting\n");
        exit(0);
    }
    else
    {
        int status;
        while(1)
        {
            pid_t result = waitpid(pid, &status, WNOHANG);
            if(result == 0)
            {
                printf("Child still running...\n");
                sleep(1);
            }
            else if(result == pid)
            {
                printf("Child exited with status %d\n", WEXITSTATUS(status));
                break;
            }
            else
            {
                perror("waitpid error");
                break;
            }
        }
    }
    return 0;
}








